﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Common;

namespace webs.website_admin.knowlegebase
{
    public partial class bannerconfigeditor : PageBase
    {
        string BannerImages = ConfigurationManager.AppSettings["BannerImages"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        { 
            Response.Expires = -1;
            PageBegin();

            /// 保存设置
            if (RequestBase.GetFormString("___EVENTTARGET") == "save")
            {
                Save();
            }

            if (!IsPostBack)
            {
                if (RequestBase.GetQueryString("otype") == "modify")
                    Initialize();
            }
        }

        #region 数据初始化
        void Initialize()
        {
            Model.PageConfig.BannerConfig _obj = BLL.PageConfig.BannerConfig.GetObjectById(RequestBase.GetQueryString("objectid"));
            BannerConfigObj = _obj; 
        }

        Model.PageConfig.BannerConfig BannerConfigObj
        {
            get
            {
                Model.PageConfig.BannerConfig _obj = new Model.PageConfig.BannerConfig();

                if (!string.IsNullOrEmpty(RequestBase.GetQueryString("objectid")))
                    _obj.ConfigId = RequestBase.GetQueryString("objectid");

                _obj.Text1 = txtText1.Value.Trim();
                _obj.ShowPositionCode = int.Parse(txtShowPositionCode.Value.Trim());
                _obj.Picture1 = txtUploadControl_BannerImages.Value.Trim().Replace("|", "");
                 
                _obj.ModifyOn = DateTime.Now;
                _obj.CreatedOn = DateTime.Now;
                _obj.ModifyBy = this.SystemUserId;
                _obj.CreatedBy = this.SystemUserId;

                return _obj;
            }
            set
            {
                if (value != null)
                {
                    txtText1.Value = value.Text1.Trim();
                    txtShowPositionCode.Value = value.ShowPositionCode.ToString();

                    if (value.Picture1 != "")
                    {
                        txtUploadControl_BannerImages.Value = "|" + value.Picture1.Trim();
                        divBannerImagesList_BannerImages.InnerHtml = "<div class=\"attachmentitems\">" + BLL.AttachmentsBase.GetIcoByExtName(value.Picture1.Trim().Split('*')[2]) + "&nbsp;&nbsp;<a href='../../" + BannerImages + value.Picture1.Trim().Split('*')[1] + "' title='单击打开文件' target='_blank'>" + value.Picture1.Trim().Split('*')[0] + "</a>&nbsp;&nbsp; <a href='javascript:void(0);' filedata='" + value.Picture1.Trim() + "' itemid='BannerImages' onclick='uploadinit($(this));'>[删除]</a></div>";
                    }
                }
            }
        } 
        #endregion

        #region 保存数据
        void Save()
        {
            string res = "";

            if (RequestBase.GetQueryString("otype").Trim() == "add")
            {
                res = BLL.PageConfig.BannerConfig.Add(BannerConfigObj);
            }

            if (RequestBase.GetQueryString("otype").Trim() == "modify")
            {
                res = BLL.PageConfig.BannerConfig.Update(BannerConfigObj);
            }

            switch (res.Split('|')[0])
            {
                case "succeeded":
                    JScript.AlertAndReturnValueToParent("保存成功!", "window.opener.LoadingData(); window.close();");
                    break;
                default:
                    JScript.Alert(res);
                    break;
            }
        }
        #endregion

    }
}
